package com.fhsk.file.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 处理日期格式化问题的帮助类
 */
public class DateUtil {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");

    private static final SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm:ss");

    private static final SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM");

    private static final SimpleDateFormat sdf5 = new SimpleDateFormat("yyyyMMddHHmm");

    private static final SimpleDateFormat sdf6 = new SimpleDateFormat("yyyyMMddHHmmss");

    private static final SimpleDateFormat sdfDateHourMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    private static final SimpleDateFormat sdfMonthAndDay = new SimpleDateFormat("MM-dd");


    private DateUtil() {
    }

    /**
     * 将形如yyyy-MM-dd HH:mm:ss字符串转化为日期形式
     *
     * @param datetime
     * @return
     */
    public synchronized static Date parseDatetime(String datetime) throws ParseException {
        return sdf.parse(datetime);
    }

    /**
     * 将形如yyyy-MM-dd字符串转化为日期形式
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public synchronized static Date parseDate(String date) throws ParseException {
        return sdf2.parse(date);
    }

    /**
     * 将形如yyyy-MM字符串转化为日期形式
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public synchronized static Date parseYearMonth(String date) throws ParseException {
        return sdf4.parse(date);
    }

    /**
     * 将形如HH:mm:ss字符串转化为日期形式
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public synchronized static Date parseTime(String time) throws ParseException {
        return sdf3.parse(time);
    }

    /**
     * 将形如yyyy-MM-dd HH:mm字符串转化为日期形式
     *
     * @param dateHourMinute
     * @return
     * @throws ParseException
     */
    public synchronized static Date parseDateHourMinute(String dateHourMinute) throws ParseException {
        return sdfDateHourMinute.parse(dateHourMinute);
    }

    /**
     * 将形如yyyyMMddHHmm字符串转化为日期形式
     *
     * @param dateHourMinute
     * @return
     * @throws ParseException
     */
    public synchronized static Date parseDHM(String dateHourMinute) throws ParseException {
        return sdf5.parse(dateHourMinute);
    }

    /**
     * 将形如yyyyMMddHHmmss字符串转化为日期形式
     *
     * @param dateHourMinute
     * @return
     * @throws ParseException
     */
    public synchronized static Date parseDHMS(String dateHourMinute) throws ParseException {
        return sdf6.parse(dateHourMinute);
    }

    /**
     * 将形如MM-dd字符串转化为日期格式
     *
     * @param monthAndDay
     * @return
     * @throws ParseException
     */
    public synchronized static Date parseMonthAndDay(String monthAndDay) throws ParseException {
        return sdfMonthAndDay.parse(monthAndDay);
    }

    /**
     * 将日期转化为形如yyyy-MM-dd HH:mm:ss格式的字符串
     *
     * @param date
     * @return
     */
    public synchronized static String formatDatetime(Date date) {
        return date == null ? "" : sdf.format(date);
    }

    /**
     * 将日期转化为形如yyyy-MM-dd格式的字符串
     *
     * @param date
     * @return
     */
    public synchronized static String formatDate(Date date) {
        return date == null ? "" : sdf2.format(date);
    }

    /**
     * 将日期转化为形如yyyy-MM格式的字符串
     *
     * @param date
     * @return
     */
    public synchronized static String formatYearMonth(Date date) {
        return date == null ? "" : sdf4.format(date);
    }

    /**
     * 将日期转化为形如yyyyMMddHHmmss格式的字符串
     *
     * @param date
     * @return
     */
    public synchronized static String formatDatetimeM(Date date) {
        return date == null ? "" : sdf6.format(date);
    }

    /**
     * 将日期转化为形如HH:mm:ss格式的字符串
     *
     * @param date
     * @return
     */
    public synchronized static String formatTime(Date date) {
        return date == null ? "" : sdf3.format(date);
    }

    /**
     * 将日期转化为形如yyyy-MM-dd HH:mm格式的字符串
     *
     * @param dateHourMinute
     * @return
     */
    public synchronized static String formatDateHourMinute(Date dateHourMinute) {
        return dateHourMinute == null ? "" : sdfDateHourMinute.format(dateHourMinute);
    }

    /**
     * 将日期转化成形如MM-dd格式的字符串
     *
     * @param monthAndDay
     * @return
     */
    public synchronized static String formatMonthAndDay(Date monthAndDay) {
        return monthAndDay == null ? "" : sdfMonthAndDay.format(monthAndDay);
    }

}